package binarytree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: 海琳琦
 * @Date: 2021/9/14 20:43
 */
public class Title101 {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val = val;
        }
        TreeNode(int val, TreeNode left, TreeNode right){
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {

    }

    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return check(root.left, root.right);
    }

    private boolean check(TreeNode l, TreeNode r){
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(l);
        queue.offer(r);
        while(!queue.isEmpty()){
            TreeNode pol1 = queue.poll();
            TreeNode pol2 = queue.poll();
            if(pol1==null&&pol2==null){
                continue;
            }
            if((pol1==null)||(pol2==null)||(pol1.val!=pol2.val)){
                return false;
            }
            queue.offer(pol1.left);
            queue.offer(pol2.right);
            queue.offer(pol1.right);
            queue.offer(pol2.left);
        }

        return true;
    }
}
